package com.tencreat.service.mall.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.*;
import com.tencreat.common.enums.OrderStatus;
import com.tencreat.common.enums.UserOrderStatus;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.TenCreatUtil;
import com.tencreat.domain.account.AccountLoginCheck;
import com.tencreat.domain.mall.MallComment;
import com.tencreat.domain.mall.dto.MallCommentDTO;
import com.tencreat.domain.mall.vo.ResMallCommentVO;
import com.tencreat.mapper.mall.MallCommentMapper;
import com.tencreat.mapper.mall.MallOrderMapper;
import com.tencreat.service.account.IAccountLoginCheckService;
import com.tencreat.service.mall.IMallCommentService;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 评论Service业务层处理
 *
 * @author tencreat
 * @date 2021-08-13
 */

@Service
@RequiredArgsConstructor
public class MallCommentServiceImpl extends ServiceImpl<MallCommentMapper, MallComment> implements IMallCommentService {

    private final MallOrderMapper orderMapper;

    /**
     * 查询评论
     *
     * @param id 评论ID
     * @return 评论
     */
    @Override
    public MallComment selectMallCommentById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询评论列表
     *
     * @param mallComment 评论
     * @return 评论
     */
    @Override
    public List<MallComment> selectMallCommentList(MallComment mallComment) {
        return baseMapper.selectList(Wrappers.query(mallComment));
    }

    /**
     * 新增评论
     *
     * @param mallCommentVO 评论
     * @return 结果
     */
    @Override
    public int insertMallComment(MallCommentDTO mallCommentVO) {
        MallComment mallComment = new MallComment();
        BeanUtils.copyProperties(mallCommentVO, mallComment);

        if (StringUtils.isEmpty(mallComment.getOrderSn())) {
            mallComment.setOrderSn(TenCreatUtil.getOrderNo());
        } else {
            //修改订单状态
            orderMapper.updateStatus(UserOrderStatus.EVALUATED.getValue(), OrderStatus.OK.getValue(), mallComment.getOrderSn());
        }
        if (null == mallComment.getStar()) {
            mallComment.setStar(5);
        }

        mallComment.setCreateTime(LocalDateTime.now());
        mallComment.setOrderSn(mallCommentVO.getOrderGoodsSn());
        return baseMapper.insert(mallComment);
    }

    /**
     * 修改评论
     *
     * @param mallComment 评论
     * @return 结果
     */
    @Override
    public int updateMallComment(MallComment mallComment) {
        mallComment.setUpdateTime(LocalDateTime.now());
        mallComment.setAdminTime(LocalDateTime.now());
        return baseMapper.updateById(mallComment);
    }

    /**
     * 批量删除评论
     *
     * @param ids 需要删除的评论ID
     * @return 结果
     */
    @Override
    public int deleteMallCommentByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除评论信息
     *
     * @param id 评论ID
     * @return 结果
     */
    @Override
    public int deleteMallCommentById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public IPage<ResMallCommentVO> queryPage(ReqStringParam param) {

        IPage<ResMallCommentVO> resMallCommentVOPage = new Page<>(param.getPageNum(), param.getPageSize());
        Long goodsId= Long.valueOf(param.getCommon());
        return baseMapper.queryPage(resMallCommentVOPage, goodsId,param);

/*        return this.lambdaQuery()
                .eq(MallComment::getGoodsId, Integer.valueOf(param.getCommon()))
                .eq(MallComment::getType, 0)
                .eq(MallComment::getIsHide, 0)
                .orderByDesc(MallComment::getCreateTime)
                .page(new Page<>(param.getPageNum(), param.getPageSize()))
                .convert(i -> {
                    ResMallCommentVO result = new ResMallCommentVO();
                    BeanUtils.copyProperties(i, result);
                    AccountLoginCheck one = SpringUtil.getBean(IAccountLoginCheckService.class).lambdaQuery()
                            .select(AccountLoginCheck::getUsername)
                            .eq(AccountLoginCheck::getUuid, i.getUuid())
                            .oneOpt()
                            .orElse(new AccountLoginCheck().setUsername("nameless"));
                    result.setUserName(one.getUsername());
                    return result;
                });*/
    }
}
